<?php

namespace Beerus\Modules\VirtualPhone\Controllers;

use App\Pockets\GridDataModel as pGridDataModel;
use App\Http\Controllers\Controller;
use Doraemon\service\BusinessUser\EncryptPhone;
use hhz\constant\basic\UserExtendPhoneScene;
use Illuminate\Http\Request;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Doraemon\service\User;
use Doraemon\model\VirtualPhone\VirtualPhoneRecord;
use Doraemon\cache\UserNick;
use Doraemon\model\VirtualPhone\VirtualPhoneUseRecord;
use Encore\Admin\Widgets\Tab;
use Doraemon\model\VirtualPhone\DesignerVirtualUserRecord;
use Doraemon\model\VirtualPhone\DesignerVirtualPhoneRecord;

/**
 * Undocumented class
 */
class DesignerVirtualPhoneUseController extends Controller
{
    const PAGE_SIZE = 20;

    const CALL_TYPE_CN_ARR = ['', '设计师拨打', '用户回拨', '短信发送', '短信接收', '呼叫拦截', '短信收发拦截'];
    const UNCONNECT_CN_ARR =
        ['', '正常通话', '黑名单拦截', '无绑定关系', '呼叫限制', '其他', '未接通'];

    const CONTROL_MSG_MAP = ['OK' => 'OK(正常)', 'NO_SUBS_EXIST' => 'NO_SUBS_EXIST(无绑定关系)', 'RISK_INTERCEPTION' => 'RISK_INTERCEPTION(风控拦截)'];

    public function __construct()
    {
        parent::__construct();
        //隐藏新增按钮--避免隐藏动画出现
        // Admin::style('.grid-create-btn{display:none;}');
        //移除新增按钮
        // Admin::script('$('.pjax-container').attr('id','pjax-container');$('.grid-create-btn').remove();');
    }

    public function index(Request $request, Content $content)
    {
        $request_data = $request->all();

        $page_size = $request->input('per_page', self::PAGE_SIZE);
        $page = $request->input('page', 1);

        $condition = [];
        if (!empty($request_data['designer_uid'])) {
            $condition['designer_uid'] = $request_data['designer_uid'];
        }
        if (!empty($request_data['uid'])) {
            $condition['user_uid'] = $request_data['uid'];
        }
        if (!empty($request_data['user_phone'])) {
            $condition['user_phone'] = EncryptPhone::getHiddenPhone($request_data['user_phone']);
        }
        if (!empty($request_data['designer_phone'])) {
            $condition['designer_phone'] = EncryptPhone::getHiddenPhone($request_data['designer_phone']);
        }
        if (!empty($request_data['virtual_phone'])) {
            $condition['virtual_phone'] = $request_data['virtual_phone'];
        }
        //时间区间筛选
        if (!empty($request_data['time'])) {
            //开始时间
            if(!empty($request_data['time']['start'])) {
                $condition['call_time[>=]'] = strtotime($request_data['time']['start']);
            }
            //结束时间, 最后一天
            if (!empty($request_data['time']['end'])) {
                $condition['call_time[<=]'] = strtotime($request_data['time']['end']) + 86400;
            }
        }
        //电话类型筛选
        if (!empty($request_data['type'])) {
            $condition['call_type'] = $request_data['type'];
        }

        $condition['ORDER'] = ['id' => 'DESC'];
        $condition['LIMIT'] = [
            ($page - 1) * $page_size,
            $page_size
        ];
        //列表查询
        $lists = DesignerVirtualUserRecord::select(['id', 'user_uid', 'designer_uid','user_phone','designer_phone','virtual_phone', 'call_time',
            'total_time', 'call_type', 'subs_id','control_msg',
            'unconnected_status'], $condition);//, 'useRecord.user_phone', 'useRecord.virtual_phone'
        $subsIdArr = array_column($lists,'subs_id');
        //根据subsId去记录表查询手机号相关的记录
        $recordInfo = DesignerVirtualPhoneRecord::select(['id','hidden_user_phone','hidden_designer_phone','subs_id'],['subs_id' => $subsIdArr]);
        $recordInfoMap = array_column($recordInfo,null,'subs_id');
        $total = 0;
        if (!empty($lists)) {
            unset($condition['ORDER']);
            unset($condition['LIMIT']);
            $total = DesignerVirtualUserRecord::count($condition);
        }



        $gridModel = new pGridDataModel($lists, $total, [
            'perPage' => $page_size
        ]);
        $gridList = Admin::grid(
            $gridModel,
            function (Grid $grid) use ($gridModel,$recordInfoMap) { //传入参数， 避免多次遍历
                $grid->paginate($gridModel->getPerPage());
                $grid->disableExport();
                $grid->disableRowSelector();

                $grid->filter(function (Grid\Filter $filter) {
                    $filter->disableIdFilter();
                    $filter->column(1 / 2, function ($filter) {
                        $filter->equal('designer_uid', '设计师uid')->placeholder('请输入设计师uid');
                        $filter->equal('uid', '用户uid')->placeholder('请输入用户的uid');
                    });

                    $filter->column(1 / 2, function ($filter) {
//                        $filter->equal('designer_phone', '设计师手机号')->placeholder('请输入设计师手机号');
//                        $filter->equal('user_phone', '用户手机号')->placeholder('请输入用户的手机号');
                        $filter->equal('virtual_phone', '虚拟手机号')->placeholder('请输入虚拟手机号');
                    });

                    $filter->column(1 / 2, function ($filter) {
                        // 设置datetime类型
                        $filter->between('time', '时间')->date();
                        // 电话类型
                        $filter->equal('type', '电话类型')->select(['1' => '设计师回拨','2' => '用户呼叫']);
                    });



                });

                $grid->column('id', 'Id');

                $grid->column('designer_nick', '设计师昵称')->display(function () {
                    return UserNick::uid2nick($this->designer_uid);
                });

                $grid->colunm('designer_uid', '设计师uid')->display(function(){
                    return $this->designer_uid;
                });

                $grid->column('decrypt_designer_phone', '设计师手机号码')->display(function () use ($recordInfoMap) {
                    return $recordInfoMap[$this->subs_id]['hidden_designer_phone'];
                });

                $grid->column('user_nick', '用户昵称')->display(function () {
                    return UserNick::uid2nick($this->user_uid);
                });
                $grid->column('user_uid', '用户uid');

                $grid->column('user_phone', '用户真实号码')->display(function ()use($recordInfoMap){
                    return $recordInfoMap[$this->subs_id]['hidden_user_phone'];
                });
                $grid->column('virtual_phone', '虚拟号码');


                $grid->column('call_type', '电话类型')->display(function () {
                    return self::CALL_TYPE_CN_ARR[$this->call_type];
                });

                $grid->column('unconnected_status', '接通情况')->display(function () {
                    return self::UNCONNECT_CN_ARR[$this->unconnected_status];
                });
                $grid->column('control_msg', '附加信息')->display(function (){
                    if (isset(self::CONTROL_MSG_MAP[$this->control_msg])) {
                        return self::CONTROL_MSG_MAP[$this->control_msg];
                    }
                    return $this->control_msg;
                });

                $grid->column('call_time', '拨打时间')->display(function () {
                    return date('Y-m-d H:i:s', $this->call_time);
                });
                $grid->column('total_time', '通话时长')->display(function () {
                    if($this->total_time < 60) {
                        return $this->total_time.'秒';
                    }  else {
                        return (int)($this->total_time / 60) . '分'. ($this->total_time % 60) . '秒';
                    }
                });
                $grid->disableActions();
            }
        );

        $gridList->disableCreateButton();

        return $content
            ->header('设计师虚拟手机号使用记录')
            ->row($gridList);
    }
}
